from matplotlib import pyplot
import numpy as np
import math

def loadData(name):
    datos=[]
    for line in file(name):
        line = line.replace('\n','')
        val=line.split(' ')
        res=[]
        for v in val:
            if v!='':
                res.append(float(v))
        datos.append(res)

    return datos


def histogram(minValue,maxValue,values, nhist):
    #maxValue=2.5#max(values)
    #minValue=-1.0#min(values)
    suma=0.0
    
    dist = np.zeros((nhist+1))
    count = np.zeros((nhist+1))
    step=(maxValue-minValue)/float(nhist)
    for val in values:
        if val>=minValue and val<=maxValue:
            dnd=int(math.floor((val-minValue)/step))
            dist[dnd]+=val
            count[dnd]+=1.0
    
    #Normalizacion
    for i in range(1,len(count)):
        suma+=step*(count[i]+count[i-1])*0.5
    x=[]
    y=[]
    for i in range(len(count)):
        x.append(minValue+float(i)*step)
        y.append(count[i]/suma)
    return [x,y]

#name='/home/jonk/DATOS/resultados/confinado/monodisperse/u30/L0.5/momentos.dat'
dir='/home/jonk/DATOS/resultados/confinado/polydisperse/u100/L0.2/'
name='momentos.dat'
datos=loadData(dir+name)

dx=[]
dy=[]
dz=[]
dtotal=[]
cual=0
npuntos=200
for dt in datos:
    dx.append(dt[0])
    dy.append(dt[1])
    dz.append(dt[2])
    dtotal.append(math.sqrt(dt[0]*dt[0]+dt[1]*dt[1]))
                
[x1,y1] = histogram(-1.0,1.0,dx, npuntos)
[x2,y2] = histogram(-1.0,1.0,dy, npuntos)
[x3,y3] = histogram(0.,3.,dz, npuntos)
#[x4,y4] = histogram(dtotal, npuntos)

f1 = open(dir+'Momentx.dat','w')
f2 = open(dir+'Momenty.dat','w')
f3 = open(dir+'Momentz.dat','w')
#f4 = open(dir+'MomentTot.dat','w')
for i in range(npuntos):
    f1.write(str(x1[i])+' '+str(y1[i])+'\n')
    f2.write(str(x2[i])+' '+str(y2[i])+'\n')
    f3.write(str(x3[i])+' '+str(y3[i])+'\n')
 #   f4.write(str(x4[i])+' '+str(y4[i])+'\n')
f1.close()
f2.close()
f3.close()
#f4.close()


pyplot.plot(x1,y1,'r-')
pyplot.plot(x2,y2,'g-')
pyplot.plot(x3,y3,'b-')
#pyplot.plot(x4,y4,'ro')
#pyplot.plot(dy,dx,'o')
#pyplot.xscale('log')
pyplot.show()